package com.zhou.jiancai.activity;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zhou.jiancai.R;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2017/7/19.
 */

public class LocationAndSensorActivity extends Activity implements View.OnClickListener{
    private static final String Tag = "a";
    private LocationManager lm;
    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_sensor);
        Button bt_gps = (Button) findViewById(R.id.bt_gps);
        Button bt_accelerate = (Button) findViewById(R.id.bt_accelerate);
        Button bt_wifi = (Button) findViewById(R.id.bt_wifi);
        Button bt_network = (Button) findViewById(R.id.bt_network);
        Button bt_jizhan = (Button) findViewById(R.id.bt_jizhan);
        bt_gps.setOnClickListener((View.OnClickListener) this);
        bt_wifi.setOnClickListener(this);
        bt_network.setOnClickListener(this);
        bt_jizhan.setOnClickListener(this);
        bt_accelerate.setOnClickListener(this);
        //test.main();
        //获取gps  数据
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);


        //


        //

    }

    private SensorEventListener mysensorlistener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            Toast.makeText(LocationAndSensorActivity.this, "sensorX:" + event.values[0] + "sensorY:" + event.values[1] + "sensorZ:" + event.values[2], Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };


    private LocationListener myLocationListener = new LocationListener() {

        private double altitude;

        @Override
        public void onLocationChanged(Location location) {
            if (ActivityCompat.checkSelfPermission(LocationAndSensorActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(LocationAndSensorActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            Location lastlocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            double lastlatitude=  lastlocation.getLatitude();
            double lastlongtude=lastlocation.getLongitude();


            double latitude = location.getLatitude();
            double longtude = location.getLongitude();

            double distance=Math.sqrt((lastlatitude-latitude)*(lastlatitude-latitude)+(lastlongtude-longtude)*(lastlongtude-longtude));


            float accuracy = location.getAccuracy();
            location.getBearing();
            float speed = location.getSpeed();
            location.getTime();
            location.getElapsedRealtimeNanos();
            if (location.hasAltitude()){
                //高度
                altitude = location.getAltitude();

            }

            Log.d(Tag, "latitude:" + latitude + "longtude:" + longtude + "accurcy:" + accuracy);
            Toast.makeText(LocationAndSensorActivity.this, "latitude:" + latitude + "longtude:" + longtude + "accurcy:" + accuracy + "distance:" + distance+"altitude:"+altitude, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_gps:
                if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 250, 0, myLocationListener);//这里采用的gpsprovider更精确 但是室内不能用

                break;
            case R.id.bt_wifi:
                WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

                break;
            case R.id.bt_network:
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);//这里采用的networkprovider


                break;
            case R.id.bt_jizhan:
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                String operator = telephonyManager.getNetworkOperator();
                String mcc = operator.substring(0, 3);
                String mnc = operator.substring(3);

                GsmCellLocation location = (GsmCellLocation) telephonyManager.getCellLocation();
                int cid = location.getCid();
                int lac = location.getLac();
                JSONObject tower = new JSONObject();

                break;
            case R.id.bt_accelerate:
                //获取加速度的值

                sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//获取传感器的manager
                List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER
                );//获取传感器的集合
                Sensor sensor = sensors.get(0);//获取加速度传感器
                sensorManager.registerListener(mysensorlistener, sensor, SensorManager.SENSOR_DELAY_NORMAL);//加速度传感器注册监听 这个注意不要选fast，会崩溃，  在销毁时取消注册

                break;


        }

    }

    @Override
    public void onBackPressed() {
        if (sensorManager !=null) {
            sensorManager.unregisterListener(mysensorlistener);
        }

        super.onBackPressed();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sensorManager !=null && lm !=null) {
            sensorManager.unregisterListener(mysensorlistener);
            lm.removeUpdates(myLocationListener);
        }



    }
}



